package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.javaee.domain.Course;
import com.example.javaee.domain.Grade;
import com.example.javaee.domain.UserCourse;
import com.example.javaee.exception.CourseAdminException;
import com.example.javaee.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseService courseService;

    @GetMapping("/getCourseList")
    public List<Course> getCourseList( String college,  String major,  long grade) {
        List<Course> result = new ArrayList<>();
        result = courseService.getCourseList(college, major, grade);
        return result;
    }

    @GetMapping("/findCourseByName")
    public List<Course> adminGetCourseList(String courseName) {
        List<Course> result = new ArrayList<>();
        result = courseService.adminGetCourseList(courseName);
        return result;
    }

    //分页获取所有课程，不含课程详细
    @GetMapping("/findCourse")
    public ResponseEntity<IPage<Course>> getCourses(int pageNum,
                                                    int pageSize,
                                                    Long isAll,
                                                    String courseName,
                                                    String college,
                                                    String major,
                                                    Long grade,
                                                    Float credit,
                                                    Long classification) throws CourseAdminException {
        Map<String, Object> condition = new HashMap<>();
        if (courseName != null) {
            condition.put("courseName", courseName);
        }
        if (college != null) {
            condition.put("college", college);
        }
        if (major != null) {
            condition.put("major", major);
        }
        if (grade != null) {
            condition.put("grade", grade);
        }
        if (credit != null) {
            condition.put("credit", credit);
        }
        if (classification != null) {
            condition.put("classification", classification);
        }
        IPage<Course> result = courseService.findCourse(condition, pageNum, pageSize, isAll);
        return ResponseEntity.ok(result);
    }

    //根据课头号获取该课程名下的课程班级
    @GetMapping("/{courseNumber}")
    public ResponseEntity<List<Course>> getCourseDetails(@PathVariable Long courseNumber) throws CourseAdminException {
        List<Course> result = courseService.getCourseDetails(courseNumber);
        return ResponseEntity.ok(result);
    }

    //根据用户id获取该用户的所有选课信息
    @GetMapping("/select/{userId}")
    public List<Course> getSelectedCourses(@PathVariable Long userId) throws CourseAdminException {
        List<Course> result = courseService.getSelectedCourses(userId);
        return result;
    }

    //选课
    @PostMapping("/select")
    public ResponseEntity<Void> selectCourse(@RequestBody UserCourse body) throws CourseAdminException {
        courseService.selectCourse(body.getUserId(), body.getCourseId());
        return ResponseEntity.ok().build();
    }

    //撤课
    @DeleteMapping("/select")
    public ResponseEntity<Void> undoSelection(@RequestBody UserCourse body) throws CourseAdminException {
        courseService.undoSelection(body.getUserId(), body.getCourseId());
        return ResponseEntity.ok().build();
    }

    /**
     * 课程增删改操作
     */
    //增添课程
    @PostMapping("/addCourse")
    public ResponseEntity<Void> addCourse(@RequestBody Course course) throws CourseAdminException {
        courseService.addCourse(course);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/deleteCourse")
    public ResponseEntity<Void> deleteCourse(Long id) throws CourseAdminException {
        courseService.deleteCourse(id);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/update/{id}")
    public ResponseEntity<Void> updateCourse( @PathVariable Long id, @RequestBody Course course) throws CourseAdminException {
        courseService.updateCourse(id, course);
        return ResponseEntity.ok().build();
    }

}
